<?php

include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/class/Model/Model.userMemory.php");
include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/inc/usedb_inc.php");

class DAL_danciOperator {

    public static function select($BModel_danci, $orderby, $order) {
        //$p = array(":userid" => $BModel_danci->userid);
        $p = array();
        $sql = "select *,danci.wordid from danci left join userMemory on danci.wordid=userMemory.wordid ";
        if(!empty($BModel_danci->userid))
        {
        	$sql.=" and userid=:userid ";
                $p[":userid"] = $BModel_danci->userid;
        }
        $sql.=" where 1=1 ";
        if (!empty($BModel_danci->lesson)) {
            $sql.=" and danci.lesson=:lesson ";
            $p[":lesson"] = $BModel_danci->lesson;
        }
        if (!empty($BModel_danci->bookid)) {
            $sql.=" and danci.bookid=:bookid ";
            $p[":bookid"] = $BModel_danci->bookid;
        }
        if (!empty($BModel_danci->username)) {
            $sql.=" and userMemory.username=:username ";
            $p[":username"] = $BModel_danci->username;
        }
        if (!empty($BModel_danci->memory)) {
            $sql.=" and userMemory.memory=:memory ";
            $p[":memory"] = $BModel_danci->memory;
        }
        $sql.=$orderby . " " . $order;

        $Helper = new SqliteHelper();
        return $Helper->Query($sql, $p);
    }

    public static function selectRandom($BModel_danci, $random, $orderby, $order) {
        //$p = array(":userid" => $BModel_danci->userid);
        $sql = "select *,danci.wordid from danci left join userMemory on danci.wordid=userMemory.wordid where 1=1";

        $randomstr = "-1";
        foreach ($random as $s) {
            $randomstr.=",$s";
        }
        $randomstr = "($randomstr)";

        $sql.=" and danci.wordid in $randomstr ";
        $sql.=$orderby . " " . $order;

        $Helper = new SqliteHelper();
        return $Helper->Query($sql, null);
    }

    public static function GetWordBywid($wordid) {
        $p = array(":wordid" => $wordid);
        $sql = "select * from danci where danci.wordid=:wordid";
        $Helper = new SqliteHelper();
        return $Helper->Query($sql, $p);
    }

    //随机抽取N个单词
    public static function GetRandomWordid($count) {
        $sql = "SELECT wordid  FROM danci ORDER BY RANDOM() LIMIT $count";
        $Helper = new SqliteHelper();
        $result = $Helper->Query($sql, null);

        $arrayid = array();
        foreach ($result as $value) {
            $arrayid[] = $value["wordid"];
        }

        return $arrayid;
    }
    
    //随机抽取N个单词
    public static function GetRandomWordidInBook($count,$bid) {
        $p = array(":bookid" => $bid);
        $sql = "SELECT wordid  FROM danci where bookid=:bookid ORDER BY RANDOM() LIMIT $count";
        $Helper = new SqliteHelper();
        $result = $Helper->Query($sql, $p);

        $arrayid = array();
        foreach ($result as $value) {
            $arrayid[] = $value["wordid"];
        }
        return $arrayid;
    }

    //随机抽取N个单词
    public static function GetRandomOption($wordid,$count) {
        $p = array(":wordid" => $wordid);
        $sql = "SELECT *  FROM danci where wordid<>:wordid ORDER BY RANDOM() LIMIT $count";
        $Helper = new SqliteHelper();
        $option = $Helper->Query($sql, $p);
        
        $sql = "SELECT *  FROM danci where wordid=:wordid";
        $correct = $Helper->Query($sql, $p);
        $option[]=$correct[0];
        
        return $option;
    }
}